//===================================================================================
// Microsoft patterns & practices
// Composite Application Guidance for Windows Presentation Foundation and Silverlight
//===================================================================================
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===================================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===================================================================================
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Windows.Input;
using System.Collections.Generic;
//using MSAP.Framework.Controls.Commands;

namespace MSAP.Framework.Controls
{
    /// <summary>
    /// Custom Tab control with animations.
    /// </summary>
    /// <remarks>
    /// This customization of the TabControl was required to create the animations for the transition 
    /// between the tab items.
    /// </remarks>
    public class AnimatedTabControl : TabControl
    {
        //public static readonly RoutedEvent SelectionChangingEvent = EventManager.RegisterRoutedEvent(
        //    "SelectionChanging", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(AnimatedTabControl));

        public static readonly DependencyProperty CloseAllCommandProperty = DependencyProperty.Register("CloseAllCommand", typeof(ICommand), typeof(AnimatedTabControl));
        public static readonly DependencyProperty HasMoreThanTwoItemsProperty = DependencyProperty.Register("HasMoreThanTwoItems", typeof(bool), typeof(AnimatedTabControl));


        //$$$ FB Never used private DispatcherTimer timer;
        //$$$ FB never used private SelectionChangedEventArgs lastArgs;

        public AnimatedTabControl()
        {
            DefaultStyleKey = typeof(AnimatedTabControl);
        }

        public ICommand CloseAllCommand
        {
            get { return (ICommand)GetValue(CloseAllCommandProperty); }
            set { SetValue(CloseAllCommandProperty, value); }
        }

        public bool HasMoreThanTwoItems
        {
            get { return (bool)GetValue(HasMoreThanTwoItemsProperty); }
            set { SetValue(HasMoreThanTwoItemsProperty, value); }
        }

        //public event RoutedEventHandler SelectionChanging
        //{
        //    add { AddHandler(SelectionChangingEvent, value); }
        //    remove { RemoveHandler(SelectionChangingEvent, value); }
        //}

        //protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        //{
        //    if (e.RemovedItems.Count > 0)
        //    {
        //        this.RaiseSelectionChangingEvent();

        //        this.StopTimer();
        //        this.lastArgs = e;

        //        this.timer = new DispatcherTimer
        //        {
        //            Interval = new TimeSpan(0, 0, 0, 0, 500)
        //        };

        //        this.timer.Tick += this.Timer_Tick;
        //        this.timer.Start();
        //    }
        //}

        // //This method raises the Tap event
        //private void RaiseSelectionChangingEvent()
        //{
        //    var args = new RoutedEventArgs(SelectionChangingEvent);
        //    RaiseEvent(args);
        //}

        //private void StopTimer()
        //{
        //    if (this.timer != null)
        //    {
        //        this.timer.Stop();
        //        this.timer = null;
        //    }
        //}

        //private void Timer_Tick(object sender, EventArgs e)
        //{
        //    this.StopTimer();
        //    base.OnSelectionChanged(this.lastArgs);
        // }

        void OnCloseCommandExecute(object sender, ExecutedRoutedEventArgs e)
        {
            TabItem item = e.Source as TabItem;
            
            if (item != null)
                Items.Remove(item);
            e.Handled = true;
        }

        void CloseCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
    }
}
